package com.da.web.school_major.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.da.common.Result;
import com.da.web.school_collage.entity.SchoolCollage;
import com.da.web.school_collage.service.SchoolCollageService;
import com.da.web.school_major.entity.MajorList;
import com.da.web.school_major.entity.SchoolMajor;
import com.da.web.school_major.service.SchoolMajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author da
 * @time 2023/12/5 上午 11:13
 */
@RestController
@RequestMapping("/api/major")
public class SchoolMajorController {
    @Autowired
    private SchoolMajorService schoolMajorService;
    @Autowired
    private SchoolCollageService schoolCollageService;

    //新增
    @PostMapping
    public Result add(@RequestBody SchoolMajor schoolMajor) {
        boolean save = schoolMajorService.save(schoolMajor);
        if (save) {
            return Result.success("新增成功");
        }
        return Result.error("新增失败");
    }

    //编辑
    @PutMapping
    public Result edit(@RequestBody SchoolMajor schoolMajor) {
        boolean save = schoolMajorService.updateById(schoolMajor);
        if (save) {
            return Result.success("编辑成功");
        }
        return Result.error("编辑失败");
    }

    //删除
    @DeleteMapping("/{majorId}")
    public Result delete(@PathVariable("majorId") Long majorId) {
        boolean b = schoolMajorService.removeById(majorId);
        if (b) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    //查询专业列表
    @GetMapping("/list")
    public Result getList(MajorList majorList) {
        IPage<SchoolMajor> list = schoolMajorService.getList(majorList);
        return Result.success("查询成功", list);
    }

    //查询学院列表
    @GetMapping("/getCollageList")
    public Result getCollageList() {
        List<SchoolCollage> list = schoolCollageService.list();
        return Result.success("查询成功", list);
    }


}
